home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / extract / interp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-23  |  3.2 KB  |  163 lines

  1. /*
  2.      extract - A network log processor
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. interp.c - 03/20/93
  8.  
  9. */
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include "extract.h"
  14. #include "parser.h"
  15. #include "y.tab.h"
  16. #include "interp.h"
  17.  
  18. #define CONTINUE 0
  19. #define RETURN 1
  20.  
  21. static unsigned long evaluate(struct tcpsynout *, struct parsenode *);
  22. static unsigned long checkcond(unsigned long, unsigned long, int);
  23. static int perform(struct tcpsynout *, struct actionlist *);
  24. extern void writebuf(struct tcpsynout *);
  25.  
  26. void
  27. interp(struct tcpsynout *rec, struct parsetree *pt)
  28. {
  29.      struct parsetree *rove;
  30.      int status;
  31.  
  32.      for(rove = pt;rove;rove=rove->next){
  33.       if(!rove->conditions || evaluate(rec, rove->conditions)){
  34.            status = perform(rec, rove->actions);
  35.            if(status == RETURN)
  36.             return;
  37.       }
  38.      }
  39. }
  40.  
  41. unsigned long
  42. evaluate(struct tcpsynout *rec, struct parsenode *pn)
  43. {
  44.      unsigned long lhs, rhs;
  45.      struct tm *tmb;
  46.  
  47.      if(pn->nodetype == KEY){
  48.       switch(pn->nodeval){
  49.       case SRCADDR:
  50.       case SRCNET:
  51.            return rec->ipsrcaddr;
  52.            break;
  53.       case DSTADDR:
  54.       case DSTNET:
  55.            return rec->ipdstaddr;
  56.            break;
  57.       case SRCPORT:
  58.            return rec->tcpsrcport;
  59.            break;
  60.       case DSTPORT:
  61.            return rec->tcpdstport;
  62.            break;
  63.       case DATE:
  64.            return rec->tp.tv_sec;
  65.            break;
  66.       case TIME:
  67.            tmb = localtime(&rec->tp.tv_sec);
  68.            return tmb->tm_hour*3600 + tmb->tm_min*60 + tmb->tm_sec;
  69.            break;
  70.       default:
  71.            /* hmmmm... */
  72.            break;
  73.       }
  74.      }
  75.      else if(pn->nodetype == VAL)
  76.       return pn->nodeval;
  77.      else if(pn->nodetype == UOPER){
  78.       rhs = evaluate(rec, pn->rhs);
  79.       switch(pn->nodeval){
  80.       case '!':
  81.            return !rhs;
  82.            break;
  83.       default:
  84.            break;
  85.       }
  86.      }
  87.      else {
  88.       switch(pn->nodeval){
  89.       case OR:
  90.            if((lhs = evaluate(rec, pn->lhs)))
  91.             return lhs;
  92.            else
  93.             return evaluate(rec, pn->rhs);
  94.            break;
  95.       case AND:
  96.            if(!(lhs = evaluate(rec, pn->lhs)))
  97.             return lhs;
  98.            else
  99.             return evaluate(rec, pn->rhs);
  100.            break;
  101.       case MASKOPER:
  102.            lhs = evaluate(rec, pn->lhs);
  103.            rhs = evaluate(rec, pn->rhs);
  104.            return lhs & rhs;
  105.            break;
  106.       default:
  107.            lhs = evaluate(rec, pn->lhs);
  108.            rhs = evaluate(rec, pn->rhs);
  109.            return checkcond(lhs, rhs, pn->nodeval);
  110.       }
  111.      }
  112.      return 0;
  113. }
  114.      
  115. unsigned long
  116. checkcond(unsigned long lhs, unsigned long rhs, int cond)
  117. {
  118.      switch(cond){
  119.      case '=':
  120.       return lhs == rhs;
  121.       break;
  122.      case '>':
  123.       return lhs > rhs;
  124.       break;
  125.      case '<':
  126.       return lhs < rhs;
  127.       break;
  128.      case NEQ:
  129.       return lhs != rhs;
  130.       break;
  131.      case LEQ:
  132.       return lhs <= rhs;
  133.       break;
  134.      case GEQ:
  135.       return lhs >= rhs;
  136.       break;
  137.      }
  138.      return 0;
  139. }
  140.  
  141. int
  142. perform(struct tcpsynout *rec, struct actionlist *al)
  143. {
  144.      int topaction = CONTINUE;
  145.      struct actionlist *rove;
  146.  
  147.      for(rove=al;topaction != RETURN && rove;rove=rove->next)
  148.       switch(rove->action){
  149.       case PRINT:
  150.            writebuf(rec);
  151.            break;
  152.       case NEXT:
  153.            topaction = RETURN;
  154.            break;
  155.       default:
  156.            /* Hmmmm */
  157.            break;
  158.       }
  159.  
  160.      return topaction;
  161. }
  162.  
  163.